<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="purchaseOrder">
        <el-input v-model="queryParams.purchaseOrder" placeholder="请输入订单编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="供应商" prop="supplierId">
        <el-input v-model="queryParams.supplierId" placeholder="请输入供应商" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="税率" prop="requisitionTableTax">
        <el-input v-model="queryParams.requisitionTableTax" placeholder="请输入税率" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="业务员" prop="purchasePerson">
        <el-input v-model="queryParams.purchasePerson" placeholder="请输入业务员" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="订单日期" prop="purchaseDate">
        <el-date-picker clearable v-model="queryParams.purchaseDate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择订单日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="审核时间" prop="auditTime">
        <el-date-picker clearable v-model="queryParams.auditTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择审核时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="审核人" prop="auditPerson">
        <el-input v-model="queryParams.auditPerson" placeholder="请输入审核人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['purchase:purchase:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['purchase:purchase:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['purchase:purchase:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['purchase:purchase:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="purchaseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="采购订单" align="center" prop="purchaseId" />
      <el-table-column label="订单编号" align="center" prop="purchaseOrder">
        <template slot-scope="scope">
          <router-link :to="'/purchase-table/purchase/' + scope.row.purchaseId" class="link-type">
            <span>{{ scope.row.purchaseOrder }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="采购类型" align="center" prop="requisitionType">
        <template slot-scope="scope">
          <span v-if="scope.row.requisitionType == 0">
            <el-tag type="success">普通采购</el-tag>
          </span>
          <span v-if="scope.row.requisitionType == 1">
            <el-tag type="danger">紧急采购</el-tag>
          </span>

        </template>
      </el-table-column>
      <el-table-column label="供应商" align="center" prop="supplier.supplierName" width="200px" />
      <el-table-column label="税率" align="center" prop="requisitionTableTax" />
      <el-table-column label="业务员" align="center" prop="purchasePerson" />
      <el-table-column label="订单日期" align="center" prop="purchaseDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.purchaseDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="auditStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.auditStatus == 0">
            <el-tag type="danger">未审核</el-tag>
          </span>
          <span v-if="scope.row.auditStatus == 1">
            <el-tag type="success">已审核</el-tag>
          </span>

        </template>
      </el-table-column>
      <el-table-column label="审核时间" align="center" prop="auditTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="auditPerson" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <span v-if="scope.row.auditStatus == 0">
                        <el-button size="mini" type="text" icon="el-icon-circle-check" @click="handleAudit(scope.row)"
                            v-hasPermi="[' purchase:purchase:audit']">审核下单</el-button>
                    </span>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['purchase:purchase:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['purchase:purchase:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

      <el-dialog :title="title" :visible.sync="openPurchase" width="960px" append-to-body>

 <el-form :model="queryTable" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px" readonly>
      <el-form-item label="采购订单" prop="purchaseId">
        <el-input v-model="queryTable.purchaseId" clearable @keyup.enter.native="handleQuery" readonly  />
      </el-form-item>
      <el-form-item label="业务员" prop="purchasePerson">
          <span style="padding-right:55px;font-size:15px;">{{ Reqion.purchasePerson }}</span>
      </el-form-item>

      <el-form-item label="税率" prop="requisitionTableTax">
          <span style="padding-right:55px;font-size:15px;">{{ Reqion.requisitionTableTax }}</span>
      </el-form-item>
      <el-form-item   label="供应商" prop="supplierId">
        <el-select v-model="Reqion.supplierId">
          <el-option   v-for="item in supplierList"
                       :key="item.supplierName"
                       :label="item.supplierName"
                       :value="item.supplierId">
            </el-option>
        </el-select>
      </el-form-item>

        <el-form-item label="日期" prop="purchaseDate">
                    <span style="padding-right:55px;font-size:15px;">{{ Reqion.purchaseDate }}</span>
                    </el-form-item>
                    <el-form-item label="采购类型" prop="requisitionType">
          <span style="padding-right:55px;font-size:15px;">{{ Reqion.requisitionType }}</span>
      </el-form-item>
                  </el-form>


      <el-table v-loading="loading" :data="tableList" @selection-change="handleChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="采购订单数据" align="center" prop="purchaseTableId" /> -->
      <!-- <el-table-column label="采购订单id" align="center" prop="purchaseId" /> -->
      <el-table-column label="存货编码" align="center" prop="purchaseOrderNo" />
      <el-table-column label="存货名称" align="center" prop="purchaseTableName" width="200px"/>
      <el-table-column label="规格型号" align="center" prop="purchaseTableSpeion" />
      <el-table-column label="单位" align="center" prop="purchaseTableUnit" />
      <el-table-column label="数量" align="center" prop="purchaseTableNum" />
      <el-table-column label="含税单价" align="center" prop="purchaseUnitPrice" />
      <el-table-column label="单价" align="center" prop="purchasePrice" />
      <el-table-column label="总价金额" align="center" prop="purchaseTotalPrice" />
      <el-table-column label="税额" align="center" prop="purchaseTaxPrice" />
      <el-table-column label="含税总金额" align="center" prop="purchaseUnitTotalPrice" />
      <el-table-column label="税率" align="center" prop="purchaseTax" />
      <el-table-column label="计划到货日期" align="center" prop="purchaseDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.purchaseDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>


    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="Addhandle" :disabled="multiple" v-hasPermi="['requisition:requisition:audit']">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 添加或修改采购订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单编号" prop="purchaseOrder">
          <el-input v-model="form.purchaseOrder" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="供应商" prop="supplierId">
          <el-input v-model="form.supplierId" placeholder="请输入供应商" />
        </el-form-item>
        <el-form-item label="税率" prop="requisitionTableTax">
          <el-input v-model="form.requisitionTableTax" placeholder="请输入税率" />
        </el-form-item>
        <el-form-item label="业务员" prop="purchasePerson">
          <el-input v-model="form.purchasePerson" placeholder="请输入业务员" />
        </el-form-item>
        <el-form-item label="订单日期" prop="purchaseDate">
          <el-date-picker clearable v-model="form.purchaseDate" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择订单日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="审核时间" prop="auditTime">
          <el-date-picker clearable v-model="form.auditTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择审核时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核人" prop="auditPerson">
          <el-input v-model="form.auditPerson" placeholder="请输入审核人" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { auditPurchase,listPurchase, getPurchase, delPurchase, addPurchase, updatePurchase } from "@/api/purchase/purchase";
import {  listPurchasetb } from  "@/api/purchase/purchasetb";
import {listSupplier} from "@/api/supplier/supplier";
import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  import { listWarehouseDept } from "@/api/warehouseDept/warehouseDept";


export default {
  name: "Purchase",
  data() {
    return {
      warehouseDeptList:[],
      // 仓库分类树选项
      warehouseDeptOptions: [],
       // 重新渲染表格状态
       refreshTable: true,
      formWarehouse:{},
      openWarehouse:false,
      Reqion:{},
      tableList:[],
      openPurchase:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 部门树选项
      deptOptions: undefined,
      // 总条数
      total: 0,
      // 采购订单表格数据
      purchaseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        purchaseOrder: null,
        requisitionType: null,
        supplierId: null,
        requisitionTableTax: null,
        purchasePerson: null,
        purchaseDate: null,
        auditStatus: null,
        auditTime: null,
        auditPerson: null,
      },
      queryTable:{
        pageNum: 1,
        pageSize: 10,
        purchaseId: null,
        purchaseOrderNo: null,
        purchaseTableName: null,
        purchaseTableNum: null,
        purchaseUnitPrice: null,
        purchasePrice: null,
        purchaseTotalPrice: null,
        purchaseTaxPrice: null,
        purchaseUnitTotalPrice: null,
        purchaseTax: null,
        purchaseDate: null
      },
      supplierList:[],
      pruchaseData:{
        // purchaseId:"",
        // purchaseDate:"",
        // requisitionType:"",
        // supplierIds:""
      },

      rows:[],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {



  //供应商分类
  supplierLists() {
        listSupplier(null).then(response => {
          this.supplierList = response.rows;
        });
      },
    Addhandle(){
      this.pruchaseData.purchaseId=this.Reqion.purchaseId;
      this.pruchaseData.purchaseDate=this.Reqion.purchaseDate;
      this.pruchaseData.requisitionType=this.Reqion.requisitionType;
      this.pruchaseData.supplierIds=this.Reqion.supplierId;
      this.pruchaseData.purchasePerson=this.Reqion.purchasePerson;
      this.pruchaseData.requisitionTableTax=this.Reqion.requisitionTableTax;
      this.pruchaseData.orderList=this.rows
      console.log(this.pruchaseData);
      auditPurchase(this.pruchaseData).then(response => {
        this.$modal.msgSuccess("审核操作成功");
        this.openPurchase = false;
        this.getList();
    })



  },

      // 多选框选中数据
      handleChange(selection) {
      this.rows = selection.map(item => item)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    purList(purchaseId){
      getPurchase(purchaseId).then(response=>{
                this.Reqion=response.data
            })
    },
    //根据查询采购订单列表
    purchases(){
      listPurchasetb(this.queryTable).then(response => {
        this.tableList = response.rows;
        this.total = response.total;
        this.loading = false;
      })
    },
    //审核下单弹窗
    handleAudit(row){
      this.queryTable.purchaseId= row.purchaseId;
      this.purchases();//根据id查询采购订单列表
      const purchaseId=this.queryTable.purchaseId
      this.purList(purchaseId)
      this.supplierLists();
      this.openPurchase = true;
      this.title = '采购下单窗口';
    },
    /** 查询采购订单列表 */
    getList() {
      this.loading = true;
      listPurchase(this.queryParams).then(response => {
        this.purchaseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        purchaseId: null,
        purchaseOrder: null,
        requisitionType: null,
        supplierId: null,
        requisitionTableTax: null,
        purchasePerson: null,
        purchaseDate: null,
        remark: null,
        auditStatus: null,
        auditTime: null,
        auditPerson: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.purchaseId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加采购订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const purchaseId = row.purchaseId || this.ids
      getPurchase(purchaseId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改采购订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.purchaseId != null) {
            updatePurchase(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPurchase(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const purchaseIds = row.purchaseId || this.ids;
      this.$modal.confirm('是否确认删除采购订单编号为"' + purchaseIds + '"的数据项？').then(function () {
        return delPurchase(purchaseIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('purchase/purchase/export', {
        ...this.queryParams
      }, `purchase_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
